var baowais = [];
//加的

var search_data = {page:1,limit:5,
    status:stitle.value};

loadData();
function showData(arr){
    list_div.innerHTML = "";
    if(arr.length==0){
        list_div.innerHTML = "暂无数据";
        return;
    }
    var allhtml = "";
    allhtml += "<table cellpadding='0' cellspacing='0'" +
        " width='100%'><tr class='theader'>" +
        "<th>订单类型</th><th>子类型名称</th><th>厂家单号</th><th>创建时间</th>" +
        "<th>修改时间</th><th>状态</th><th>操作</th></tr>";
    for(var i=arr.length-1;i>=0;i--){
        var a = arr[i];
        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>"+a.type+"</td>" +
            "<td class='ntitle'>"+a.type_name+"</td>" +
            "<td class='ntitle'>"+a.factory_num+"</td>" +
            "<td class='ntitle'>"+a.build_time.substring(0,10)+"</td>" +
            "<td class='ntitle'>"+a.revise_time.substring(0,10)+"</td>" +
            "<td class='ntitle'>"+a.status+"</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd("+a.id+")' href='javascript:;'>编辑</a>" +
            "<a class='del_a' onclick='del("+a.id+")' href='javascript:;'>删除  </a>" +
            "<a class='edit_a' onclick='upd1("+a.id+")' href='#'>启用</a>" +
            "<a class='edit_a'  onclick='upd2("+a.id+")' href='#'>禁用</a></td> "+




            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML = allhtml;
}

function search(){
    search_data = {page:1,limit:5,
        status:stitle.value,
    }
    loadData();
}



function loadData(){
    list_div.innerHTML = "";
    loading.style.display = "block";
    ajax("/find_all_baowai",search_data,function(data){
        baowais = data;
        showData(baowais);
        loading.style.display="none";
    });
//    添了此处到to page;
    ajax("/count_all_baowai",search_data,function(data){
        // console.log(data);  33
        data = parseInt(data);
        current_page.innerHTML = search_data.page;
        total_page.innerHTML =
            (data%search_data.limit==0?
                data/search_data.limit:
                Math.floor(data/search_data.limit)+1);
    });
}
function next_page(){
    if(search_data.page<parseInt(total_page.innerHTML)) {
        search_data.page++;
        loadData();
    }
}


function pre_page(){
    if(search_data.page>=2&&search_data.page<=parseInt(total_page.innerHTML)) {
        search_data.page--;
        loadData();
    }
}

function index_page(){
    search_data.page=1;
    loadData();
}

function tail_page(){
    search_data.page=parseInt(total_page.innerHTML);
    loadData();
}


function to_page(){
    var ip = parseInt(input_page.value);
    if(ip>=1&&ip<=parseInt(total_page.innerHTML)){
        search_data.page = ip;
        loadData();
    }
}

function save(){
    ajax("/add_baowai",{
        type:type1.value,
        type_name:type_name1.value,
        factory_num:factory_num1.value,
        status:status1.value
    },function(data){
        alert("新增成功！");
        loadData();
        add_div.style.display = "none";
        list_div.style.display = "block";
        save_btn.style.display = "none";
        add_btn.style.display = "block";
    })

}


// function del(id){
//     if(!confirm("您确定要删除这个数据吗？")){
//         return;
//     }
//     var req = new XMLHttpRequest();
//     req.open("post","/del_baowai");
//     req.setRequestHeader("content-Type","application/json;charset=utf-8");
//     var data = {
//         id:id
//     }
//     req.send(JSON.stringify(data));
//     req.onreadystatechange = function(){
//         if(req.readyState==4&&req.status==200){
//             if(req.responseText.trim()=="1"){
//                 alert("删除成功！");
//             }
//
//             loadData();
//         }
//     }
// }


function del(id) {
    if (!confirm("您确定要删除这个数据吗？")) {
        return;
    }
    ajax("/del_baowai", {id: id}, function (data) {
        alert("删除成功！");
        loadData();
    });
}
function add(){
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
}
function upd1(id){
    ajax("/upd_baowai1",{
        id:id,
        status:'启用'
    },function(data){
        loadData();
    });
}
function upd2(id){
    ajax("/upd_baowai2",{
        id:id,
        status:'禁用'
    },function(data){
        loadData();
    });
}
function updsave(){
    ajax("/upd_baowai",{
        id:upd_id,
        type:type2.value,
        type_name:type_name2.value,
        factory_num:factory_num2.value,
        status:status2.value
    },function(data){
        alert("修改成功！");
        loadData();
        upd_div.style.display = "none";
        list_div.style.display = "block";
        updsave_btn.style.display = "none";
        add_btn.style.display = "block";
//                    return_btn.style.display = "none";
    })
}

function upd(id){
    upd_id=id;
    if(confirm("您确定要编辑这个数据吗？")){
        upd_div.style.display = "block";
        list_div.style.display = "none";
        updsave_btn.style.display = "block";
        add_btn.style.display = "none";
//         return_btn.style.display = "block";
    }
}

